#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <tuple>
#include <fstream>
#include <sstream>  // 必须包含这个头文件才能使用 ostringstream

using namespace std;

// 字符串相似度计算
double calculate_similarity(const string& str1, const string& str2) {
    if (str1.empty() && str2.empty()) return 1.0;
    if (str1.empty() || str2.empty()) return 0.0;
    
    int len1 = str1.length();
    int len2 = str2.length();
    int max_len = max(len1, len2);
    
    int matches = 0;
    for (int i = 0; i < min(len1, len2); ++i) {
        if (str1[i] == str2[i]) {
            matches++;
        }
    }
    
    return static_cast<double>(matches) / max_len;
}

// 数据结构表示一行数据
struct Record {
    string keyname;
    string name;
    string keyaddress;
    string address;
    double keyname_name_match;
    double keyaddress_address_match;
    double total_match;
    int newindex;
};

// 获取当前日期作为字符串
string get_today_date() {
    time_t now = time(nullptr);
    tm* ltm = localtime(&now);
    
    ostringstream oss;
    oss << (1900 + ltm->tm_year) 
        << setw(2) << setfill('0') << (1 + ltm->tm_mon)
        << setw(2) << setfill('0') << ltm->tm_mday;
    
    return oss.str();
}

// 处理Excel数据（伪代码，实际需要 Excel 库）
void process_excel(const string& input_file, const string& output_file) {
    vector<Record> records;
    
    // TODO: 使用 Excel 库（如 libxl 或 OpenXLSX）读取数据并填充 records
    
    // 计算匹配度
    for (auto& record : records) {
        record.keyname_name_match = calculate_similarity(record.keyname, record.name);
        record.keyaddress_address_match = calculate_similarity(record.keyaddress, record.address);
        
        // 计算总匹配度
        if (!record.keyname.empty()) {
            record.total_match = record.keyname_name_match;
        } else {
            record.total_match = record.keyaddress_address_match;
        }
    }
    
    // 分组并排序
    map<pair<string, string>, vector<Record*>> groups;
    for (auto& record : records) {
        groups[make_pair(record.keyname, record.keyaddress)].push_back(&record);
    }
    
    for (auto& group : groups) {
        auto& group_records = group.second;
        sort(group_records.begin(), group_records.end(), 
            [](const Record* a, const Record* b) {
                return a->total_match > b->total_match;
            });
        
        for (size_t i = 0; i < group_records.size(); ++i) {
            group_records[i]->newindex = i + 1;
        }
    }
    
    // TODO: 使用 Excel 库写入 output_file
    cout << "处理完成，结果已保存到 " << output_file << endl;
}

int main() {
    string today_date = get_today_date();
    string input_file = "clear_data1.xlsx";
    string output_file = "/Users/ayer/Desktop/cprimer-plus/Workerclear_data" + today_date + ".xlsx";
    
    try {
        process_excel(input_file, output_file);
    } catch (const exception& e) {
        cerr << "处理过程中出现错误: " << e.what() << endl;
        return 1;
    }
    
    return 0;
}